package com.alm.token.common;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.alm.token.base.BaseAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

/**
 * Created by lidengzhou on 17/6/13.
 * 下拉刷新和加载更多的管理类，默认分页是10，
 * LayoutManager为LinearLayoutManager
 */

public class RefreshLoadMoreManager<T extends BaseAdapter.BaseItem> {

    public SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private BaseAdapter<T> adapter;
    private boolean isOpenLoadMore;
    private OnLoadMoreListener onLoadMoreListener;
    private OnRefreshListener onRefreshListener;
    private OnListItemClickListener onListItemClickListener;
    private OnListItemChildClickListener onListItemChildClickListener;
    private RecyclerView.LayoutManager layoutManager;
    private int limit;
    private int emptyLayoutResId;
    private View emptyView;


    private List<T> data;
    private boolean isLoading = false;

    private RefreshLoadMoreManager(Builder builder) {

        if (builder.recyclerView == null) {
            throw new RuntimeException("RecyclerView is must setting!");
        }

        if (builder.adapter == null) {
            throw new RuntimeException("Adapter is must setting!");
        }

        /**
         * 这里特意不检测类型，直接让它报错
         * */
        adapter = (BaseAdapter<T>) builder.adapter;

        recyclerView = builder.recyclerView;
        swipeRefreshLayout = builder.swipeRefreshLayout;
        isOpenLoadMore = builder.isOpenLoadMore;
        onRefreshListener = builder.onRefreshListener;
        onLoadMoreListener = builder.onLoadMoreListener;
        onListItemClickListener = builder.onListItemClickListener;
        onListItemChildClickListener = builder.onListItemChildClickListener;
        layoutManager = builder.layoutManager;
        limit = builder.limit;
        emptyLayoutResId = builder.emptyLayoutResId;

        init();
    }

    private void init() {
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(layoutManager);

        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    if (!isLoading) {
                        if (onRefreshListener != null) {
                            isLoading = true;
                            onRefreshListener.onRefresh();
                        } else {
                            swipeRefreshLayout.setRefreshing(false);
                        }
                    } else {
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }
            });
        }


        if (onListItemClickListener != null) {
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    onListItemClickListener.onListItemClick(data.get(position), position);
                }
            });
        }

        if (onListItemChildClickListener != null) {
            adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    onListItemChildClickListener.onListItemChildClick(view, data.get(position), position);

                }
            });
        }

        if (isOpenLoadMore) {
            adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
                    recyclerView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (!isLoading) {
                                if (onLoadMoreListener != null) {
                                    isLoading = true;
                                    onLoadMoreListener.onLoadMore();
                                }
                            }
                        }
                    }, 1000);
                }
            }, recyclerView);
        } else {
            adapter.bindToRecyclerView(recyclerView);
        }

        if (emptyLayoutResId != 0) {
            adapter.setEmptyView(emptyLayoutResId);
        } else if (emptyView != null) {
            adapter.setEmptyView(emptyView);
        }
    }


    /**
     * 设置数据源
     */
    public void setData(List<T> data) {
        if (data != null) {
            this.data = data;
            adapter.setNewData(data);
            notifyChange(data.size());
        } else {
            notifyChange(0);
        }
    }


    public void addData(List<T> data) {
        if (this.data != null && data != null) {
            adapter.addData(data);
            notifyChange(data.size());
        } else {
            notifyChange(0);
        }
    }

    public void addData(T data) {
        if (this.data != null && data != null) {
            adapter.addData(data);
            notifyChange(1);
        } else {
            notifyChange(0);
        }
    }

    public List<T> getData() {
        return data;
    }

    public BaseAdapter<T> getAdapter() {
        return adapter;
    }

//    /**
//     * 设置空页面
//     * */
//    public void setEmptyLayout(@LayoutRes int emptyLayoutResId) {
//        adapter.setEmptyView(emptyLayoutResId);
//        adapter.notifyDataSetChanged();
//    }

    /**
     * 刷新页面
     */
    public void notifyDataSetChanged() {
        adapter.notifyDataSetChanged();
    }


    /**
     * 刷新失败
     */
    public void loadingFail() {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
        isLoading = false;
        adapter.loadMoreFail();
    }

    /**
     * 下拉刷新或加载更多之后调用，用于停止刷新状态
     */
    public void notifyChange(int size) {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
        isLoading = false;
        if (size < limit) {
            adapter.loadMoreEnd(true);
        } else {
            adapter.loadMoreComplete();
        }
    }


    /**
     * 列表的item点击事件回调
     */
    public interface OnListItemClickListener {
        void onListItemClick(BaseAdapter.BaseItem item, int position);
    }

    /**
     * 列表的item子View点击事件回调
     */
    public interface OnListItemChildClickListener {
        void onListItemChildClick(View view, BaseAdapter.BaseItem item, int position);
    }

    /**
     * 加载更多的接口
     */
    public interface OnLoadMoreListener {
        void onLoadMore();
    }

    /**
     * 下拉刷新的监听器
     */
    public interface OnRefreshListener {
        void onRefresh();
    }


    public static class Builder {

        private SwipeRefreshLayout swipeRefreshLayout;

        private RecyclerView recyclerView;

        private BaseAdapter<? extends BaseAdapter.BaseItem> adapter;

        private boolean isOpenLoadMore;

        private OnLoadMoreListener onLoadMoreListener;

        private OnRefreshListener onRefreshListener;

        private OnListItemClickListener onListItemClickListener;

        private OnListItemChildClickListener onListItemChildClickListener;

        private RecyclerView.LayoutManager layoutManager;

        private int limit;

        private int emptyLayoutResId;

        private View emptyView;

        public Builder(@NonNull Context context) {
            limit = 10;
            isOpenLoadMore = false;
            layoutManager = new LinearLayoutManager(context);
            ((LinearLayoutManager) layoutManager).setOrientation(LinearLayoutManager.VERTICAL);

        }

        public Builder setSwipeRefreshLayout(@NonNull SwipeRefreshLayout swipeRefreshLayout) {
            this.swipeRefreshLayout = swipeRefreshLayout;
            return this;
        }

        public Builder setRecyclerView(@NonNull RecyclerView recyclerView) {
            this.recyclerView = recyclerView;
            return this;
        }

        public Builder setAdapter(@NonNull BaseAdapter<? extends BaseAdapter.BaseItem> adapter) {
            this.adapter = adapter;
            return this;
        }

        public Builder setOpenLoadMore(boolean openLoadMore) {
            isOpenLoadMore = openLoadMore;
            return this;
        }

        public Builder setOnListItemClickListener(OnListItemClickListener onListItemClickListener) {
            this.onListItemClickListener = onListItemClickListener;
            return this;
        }

        public Builder setOnListItemChildClickListener(OnListItemChildClickListener onListItemChildClickListener) {
            this.onListItemChildClickListener = onListItemChildClickListener;
            return this;
        }

        public Builder setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
            this.onLoadMoreListener = onLoadMoreListener;
            return this;
        }

        public Builder setOnRefreshListener(OnRefreshListener onRefreshListener) {
            this.onRefreshListener = onRefreshListener;
            return this;
        }

        public Builder setLayoutManager(RecyclerView.LayoutManager layoutManager) {
            this.layoutManager = layoutManager;
            return this;
        }

        public Builder setLimit(int limit) {
            if (limit <= 0) {
                return this;
            }
            this.limit = limit;
            return this;
        }

        public Builder setEmptyLayoutResId(@LayoutRes int emptyLayoutResId) {
            if (emptyView != null) {
                emptyView = null;
            }
            this.emptyLayoutResId = emptyLayoutResId;
            return this;
        }

        public Builder setEmptyView(View emptyView) {
            if (emptyLayoutResId != 0) {
                emptyLayoutResId = 0;
            }
            this.emptyView = emptyView;
            return this;
        }

        public <F extends BaseAdapter.BaseItem> RefreshLoadMoreManager<F> build() {

            return new RefreshLoadMoreManager<F>(this);

        }
    }

}
